package com.aaa.vegetablemarketexpress.util;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @FileName: LoginFilter
 * @Description:
 * @Author: 张钰哲
 * @CreateTime: 2025/9/8 9:10
 * @Version: 1.0.0
 */

@Component
@WebFilter(filterName = "LoginFilter",urlPatterns = "/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 强制转换
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 获取请求的URI地址
        String requestURI = request.getRequestURI();
        if (requestURI.contains("html/back/login.html")
                ||requestURI.contains("html/front/login.html")
                ||requestURI.contains("html/front/register.html")
                || requestURI.contains("/user/insertUser")  // 允许注册接口
                || requestURI.contains("/user/uploadAvatar")  // 允许头像上传接口
                ||requestURI.contains("/user/login")
                ||requestURI.endsWith(".css")
                ||requestURI.endsWith(".js")
                ||requestURI.contains("/swagger-ui/index.html")
                || requestURI.contains("/api/")        // 通常API也需要特殊处理
                || requestURI.contains("/static/")     // 静态资源
                || requestURI.contains("/images/")
                || requestURI.contains(".ttf")
                || requestURI.contains(".woff")
                || requestURI.contains("html/back/register.html")
                || requestURI.contains("html/back/forgetPassword.html")
                || requestURI.contains("user/captcha")
                || requestURI.contains("user/registerAdmin")
                || requestURI.contains("user/registerUser")
                || requestURI.contains("user/verifyUser")
                || requestURI.contains("user/resetPassword")
                || requestURI.contains("html/front/forget.html")
                || requestURI.contains("/user/forgetPassword")
                // 添加对客户相关接口的放行
                || requestURI.contains("/customer/")
                || requestURI.contains("/shop/uploadBusinessLicence/*")
                || requestURI.contains("/user/sendCaptcha")
                || requestURI.contains("user/registerCaptcha")
                || requestURI.contains("user/forgetPasswordCaptcha")
                || requestURI.contains("html/front/index.html")
                || requestURI.contains("product/selectProductList")
                || requestURI.contains("product/getProductDetail")


        ) {
            // 放行
            filterChain.doFilter(request, response);
        }else {
            //获取session用户信息
            HttpSession session = request.getSession();
            Object userInfo = session.getAttribute("userInfo");
            if(userInfo==null){
                //判断为空，说明没有登录
                //重定向
                response.sendRedirect(request.getContextPath()+"/html/back/login.html");
            }else {
                filterChain.doFilter(request,response);
            }
        }
    }

    @Override
    public void destroy() {
    }
}
